﻿using DotNetProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace DotNetProject.Controllers
{
    public class HomeController : Controller
    {
        MovieViewerDb _db = new MovieViewerDb();

        public ActionResult Index()
        {
            var model = _db.Movies
                .OrderByDescending(r => r.movieReleaseDate)
                .Take(5);
            return View(model);
        }

        public ActionResult Programmatie(string strDatum = null, int movieId = -1, string movieName = null)
        {
            DateTime datum;

            if (String.IsNullOrEmpty(strDatum))
            {
                datum = DateTime.Now;
            }
            else
            {
                datum = Convert.ToDateTime(strDatum);
            }

            var model = _db.Showtimes
                .OrderBy(r => r.showtimeTime)
                .Where(r => r.showtimeTime.Day == datum.Day 
                    && r.showtimeTime.Month == datum.Month 
                    && r.showtimeTime.Year == datum.Year 
                    && r.movie.movieTitle.StartsWith(movieName));


            if (String.IsNullOrEmpty(movieName))
            {
                model = _db.Showtimes
                    .OrderBy(r => r.showtimeTime)
                    .Where(r => r.showtimeTime.Day == datum.Day 
                        && r.showtimeTime.Month == datum.Month 
                        && r.showtimeTime.Year == datum.Year);
            }
            if (movieId != -1)
            {
                model = _db.Showtimes
                    .OrderBy(r => r.showtimeTime)
                    .Where(r => r.showtimeTime.Day == datum.Day && r.showtimeTime.Month == datum.Month && r.showtimeTime.Year == datum.Year && r.movie.movieId == movieId);
            }

            if (Request.IsAjaxRequest())
            {
                return PartialView("_ProgrammatieLijst", model);
            }

            return View(model);
        }


        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
